/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.DAO.RoomDAO;
import model.DAO.RoomStatusDAO;
import model.DAO.RoomTypeDAO;
import model.entities.Room;
import model.entities.RoomStatus;
import model.entities.RoomType;

/**
 *
 * @author Hieu.Nguyen
 */
public class QuanLyPhong extends javax.swing.JPanel {

    /**
     * Creates new form QuanLyPhong
     */
    public QuanLyPhong() {
        
        initComponents();
        loadTableRoom(refreshCourseList());
        setEnableComponent(false);
        loadCBRoomStatus();
        loadCBRoomType();
    }
    public List<Room> refreshCourseList() {
        
        List<Room> roomList;
       roomList =model.DAO.RoomDAO.getInstance().getRoom(true);
        return roomList;
    }
    public void loadCBRoomType() {
            List<RoomType> listRT = RoomTypeDAO.getInstance().getRoomType(true);
            for (RoomType ro : listRT) {
            cbroomtype.addItem(ro.getRoomTypeID()+ "-" + ro.getRoomType());
        }
    }
    public void loadCBRoomStatus() {
            List<RoomStatus> listST = RoomStatusDAO.getInstance().getRoomStatus(true);
            for (RoomStatus ro : listST) {
            cbroomST.addItem(ro.getRoomStatusID()+ "-" + ro.getRoomStatusName());
        }
    }
    public void loadTableRoom(List<Room> list) {
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        String[] colNames = {"RoomID", "Name", "RoomType", "Roomstatus", "Description","NumBeds","Gia Phong"};
        dtm.setColumnIdentifiers(colNames);
        dtm.setNumRows(0);

        for (Room stud : list) {
            String[] cells = new String[7];
            cells[0] = String.valueOf(stud.getRoomID());
            cells[1] = stud.getRoomNumber();
            cells[2] = stud.getRoomType().getRoomType();
            cells[3] = stud.getRoomStatus().getRoomStatusName();
            cells[4] = stud.getDescription();
            cells[5] = String.valueOf(stud.getRoomType().getNumBeds());
            cells[6] = String.valueOf(stud.getRoomType().getGiaPhong());
            dtm.addRow(cells);
        }
    }
    public void binding() {
        try {
            int rowIndex = jTable1.getSelectedRow();
            //int id = (int) tblStudentList.getValueAt(rowIndex, 0);
            String RoomID = jTable1.getValueAt(rowIndex, 0).toString();
            txtID.setText(RoomID);
            String name = (String) jTable1.getValueAt(rowIndex, 1);
            txtName.setText(name);
            
            int roomtypeID = RoomDAO.getInstance().getRoomByID(Integer.parseInt(RoomID)).getRoomType().getRoomTypeID();
            String RoomType = RoomDAO.getInstance().getRoomByID(Integer.parseInt(RoomID)).getRoomType().getRoomType();
            cbroomtype.setSelectedItem(roomtypeID + "-" + RoomType);
           
            int roomtypeSTID = RoomDAO.getInstance().getRoomByID(Integer.parseInt(RoomID)).getRoomStatus().getRoomStatusID();
            String RoomTypeSTName = RoomDAO.getInstance().getRoomByID(Integer.parseInt(RoomID)).getRoomStatus().getRoomStatusName();
            cbroomST.setSelectedItem(roomtypeSTID+ "-" + RoomTypeSTName);
//            
            String des = (String) jTable1.getValueAt(rowIndex, 4);
            txtDescription.setText(des);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setEnableComponent(boolean isTrue) {
        txtID.setEnabled(false);
        txtName.setEnabled(isTrue);
        txtDescription.setEnabled(isTrue);
        cbroomST.setEnabled(isTrue);
        cbroomtype.setEnabled(isTrue);
        btnSave1.setEnabled(isTrue);
        btnCancel.setEnabled(isTrue);
        btnDelete.setEnabled(isTrue);
        btnEdit.setEnabled(isTrue);
        
    }
     void resetComponent() {
        txtID.setText("");
        txtName.setText("");
        txtDescription.setText("");
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        txtID = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        lbRoomID = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        btnSave1 = new javax.swing.JButton();
        cbroomtype = new javax.swing.JComboBox();
        cbroomST = new javax.swing.JComboBox();

        jPanel1.setBackground(new java.awt.Color(255, 204, 102));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setText("Danh Sach Phong");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "IDPhong", "So Phong", "IDLoaiPhong", "IDTinhTrangPhong", "Mo Ta Phong", "So Giuong", "Gia Phong"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jTable1.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                jTable1AncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTable1KeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(405, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(321, 321, 321))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(264, 264, 264))
        );

        jPanel2.setBackground(new java.awt.Color(255, 153, 255));

        btnDelete.setText("Xoa Phong");
        btnDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDeleteMouseClicked(evt);
            }
        });

        btnAdd.setText("Them Phong");
        btnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddMouseClicked(evt);
            }
        });

        btnEdit.setText("Chinh Sua TT");
        btnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEditMouseClicked(evt);
            }
        });

        txtID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIDActionPerformed(evt);
            }
        });

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        jScrollPane2.setViewportView(txtDescription);

        lbRoomID.setText("RoomID");

        jLabel3.setText("Tên phòng");

        jLabel4.setText("Kiểu phòng");

        jLabel5.setText("Tình trạng phòng");

        jLabel6.setText("Mô tả");

        btnCancel.setText("Cancel");
        btnCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCancelMouseClicked(evt);
            }
        });

        btnSave1.setText("Save");
        btnSave1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSave1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(94, 94, 94)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbRoomID)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(32, 32, 32)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtName)
                            .addComponent(txtID)
                            .addComponent(cbroomtype, 0, 191, Short.MAX_VALUE)
                            .addComponent(cbroomST, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSave1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(233, 233, 233)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEdit)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6))
                            .addComponent(lbRoomID))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(21, 21, 21)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbroomtype, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbroomST, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnSave1)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDelete)
                    .addComponent(btnAdd)
                    .addComponent(btnEdit))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1AncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_jTable1AncestorAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1AncestorAdded

    private void txtIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIDActionPerformed

    private void jTable1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyReleased
        // TODO add your handling code here:
        
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        binding();
    }//GEN-LAST:event_jTable1KeyReleased

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        
        binding();
    }//GEN-LAST:event_jTable1MouseClicked

    private void btnDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDeleteMouseClicked
        // TODO add your handling code here:
        //selected Cột trong bảng
        int rowIndex = jTable1.getSelectedRow();
        //
        String strID = (String) jTable1.getValueAt(rowIndex, 0);
        int id = Integer.parseInt(strID);
        
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int i = JOptionPane.showConfirmDialog(null, "Bạn có chắc muốn xóa phòng");
        if (i == JOptionPane.YES_OPTION) {
            
                RoomDAO.getInstance().deleteRoom(id);
            
            JOptionPane.showMessageDialog(this, "Đã xóa phòng");
            loadTableRoom(refreshCourseList());
        } else {
            remove(dialogButton);
        }
    }//GEN-LAST:event_btnDeleteMouseClicked

    private void btnCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelMouseClicked
        // TODO add your handling code here:
        setEnableComponent(false);
    }//GEN-LAST:event_btnCancelMouseClicked

    private void btnSave1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSave1MouseClicked
        // TODO add your handling code here:
        String name= txtName.getText();
        String Des = txtDescription.getText();
       //set roomtype
        String roomType = cbroomtype.getSelectedItem().toString();
        String[] arr1 = roomType.split("-");
        int idRoomType = Integer.parseInt(arr1[0]);        
        RoomType rt = RoomTypeDAO.getInstance().getRoomTypeByID((idRoomType));
        //set roomST
        String roomStatus = cbroomST.getSelectedItem().toString();
        String[] arr2 = roomStatus.split("-");
        int idRoomStatus = Integer.parseInt(arr2[0]); 
        RoomStatus rst = RoomStatusDAO.getInstance().getRoomStatusByID(idRoomStatus, true);
        
        if (txtID.getText().equals("")) {
            Room room = new Room(name, rt, rst, Des, true);
            if (RoomDAO.getInstance().createRoom(room)) {
                JOptionPane.showMessageDialog(null, "Thêm mới thành công");
            } else {
                JOptionPane.showMessageDialog(null, "Lỗi");
            }
            resetComponent();
            setEnableComponent(false);
            loadTableRoom(refreshCourseList());
        } else {
            String strIDCourse = txtID.getText();
            int id = Integer.parseInt(strIDCourse);
            Room room = new Room(id, name, rt, rst, Des, true);
            if (RoomDAO.getInstance().updateRoom(room)) {
                JOptionPane.showMessageDialog(null, "Cập nhật thành công");
            }else{
                JOptionPane.showMessageDialog(null, "Có lỗi xảy ra");
            }
            resetComponent();
            loadTableRoom(refreshCourseList());
            setEnableComponent(false);
        }
    }//GEN-LAST:event_btnSave1MouseClicked

    private void btnEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEditMouseClicked
        // TODO add your handling code here:
        setEnableComponent(true);
    }//GEN-LAST:event_btnEditMouseClicked

    private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddMouseClicked
        // TODO add your handling code here:
        resetComponent();
        setEnableComponent(true);
       
    }//GEN-LAST:event_btnAddMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSave1;
    private javax.swing.JComboBox cbroomST;
    private javax.swing.JComboBox cbroomtype;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lbRoomID;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
